<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="app">

    <!--搜索表单-->
    <el-form :inline="true" :model="brand" class="demo-form-inline">

        <el-form-item label="当前状态">
            <el-select v-model="brand.status" placeholder="当前状态" clearable>
                <el-option label="启用" value="1"></el-option>
                <el-option label="禁用" value="0"></el-option>
            </el-select>
        </el-form-item>

        <el-form-item label="企业名称">
            <el-input v-model="brand.companyName" placeholder="企业名称" clearable></el-input>
        </el-form-item>

        <el-form-item label="品牌名称">
            <el-input v-model="brand.brandName" placeholder="品牌名称" clearable></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
    </el-form>

    <!--按钮-->
    <el-row>

        <el-button type="danger" plain @click="deleteByIds()" :disabled="deleteInBatchesBtn">批量删除</el-button>
        <el-button type="primary" plain @click="addAndEditBrandDialog(0)">新增</el-button>

    </el-row>

    <!--添加和编辑数据对话框表单-->
    <el-dialog
            :title="addAndEditDialogTitle"
            :visible.sync="dialogVisible"
            width="30%"
    >

        <el-form ref="form" :model="brand" label-width="80px">
            <el-form-item label="品牌名称">
                <el-input v-model="brand.brandName"></el-input>
            </el-form-item>

            <el-form-item label="企业名称">
                <el-input v-model="brand.companyName"></el-input>
            </el-form-item>

            <el-form-item label="排序">
                <el-input v-model="brand.ordered"></el-input>
            </el-form-item>

            <el-form-item label="备注">
                <el-input type="textarea" v-model="brand.description"></el-input>
            </el-form-item>

            <el-form-item label="状态">
                <el-switch v-model="brand.status"
                           :active-value="1"
                           :inactive-value="0"
                ></el-switch>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="addAndEditBrand(brand.id)">提交</el-button>
                <el-button @click="dialogClose()">取消</el-button>
            </el-form-item>

        </el-form>

    </el-dialog>

    <!--表格-->
    <template>
        <el-table
                :data="tableData"
                height="550"
                style="width: 100%"
                @selection-change="handleSelectionChange"
        >
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    type="index"
                    width="50">
            </el-table-column>

            <el-table-column
                    prop="brandName"
                    label="品牌名称"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="companyName"
                    label="企业名称"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="ordered"
                    align="center"
                    label="排序">
            </el-table-column>
            <el-table-column
                    prop="status"
                    align="center"
                    label="当前状态">
            </el-table-column>

            <el-table-column
                    align="center"
                    label="操作">

                <template slot-scope="scope">

                    <el-row>
                        <el-button type="primary" @click="addAndEditBrandDialog(scope.row.id)">修改</el-button>
                        <el-button type="danger" @click="deleteById(scope.row.id)">删除</el-button>
                    </el-row>

                </template>

            </el-table-column>

        </el-table>
    </template>

    <!--分页工具条-->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount">
    </el-pagination>

</div>

<script src="js/vue.js"></script>
<script src="element-ui/lib/index.js"></script>
<link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">
<script src="js/axios-0.18.0.js"></script>

<script>
    new Vue({
        el: "#app",

        // 当页面挂载完成后，发送异步请求，获取数据
        mounted() {
            this.selectAll();
        },

        methods: {
            // 根据分页和条件进行查询
            selectAll() {
                axios({
                    method: "post",
                    url: "http://localhost:8080/brand-case/brand/selectByPageAndCondition?currentPage=" + this.currentPage + "&" + "pageSize=" + this.pageSize,
                    data: this.brand
                }).then(response => {
                    // { rows:[], totalCount:100 }
                    this.tableData = response.data.rows;
                    this.totalCount = response.data.totalCount;
                })
            },

            // 添加和编辑数据对话框
            addAndEditBrandDialog(id) {
                // 判断是添加还是编辑
                if (id > 0) {
                    // 修改
                    this.dialogVisible = true
                    this.addAndEditDialogTitle = '修改品牌'
                    // 数据回写
                    axios({
                        method: "get",
                        url: "http://localhost:8080/brand-case/brand/selectById?id=" + id,
                    }).then(response => {
                        this.brand = response.data[0];
                    })
                } else {
                    // 添加
                    this.dialogVisible = true
                    this.addAndEditDialogTitle = '添加品牌'
                }
            },

            // 添加和修改品牌
            addAndEditBrand(id) {
                if (id) {
                    // 修改
                    axios({
                        method: "post",
                        url: "http://localhost:8080/brand-case/brand/update",
                        data: this.brand
                    }).then(response => {
                        if (response.data === "success") {
                            // 修改成功
                            // 关闭对话框
                            this.dialogVisible = false;
                            // 刷新数据列表
                            this.selectAll();
                            // 提示修改成功
                            this.$message({
                                message: this.addAndEditDialogTitle + '成功！',
                                type: 'success'
                            });
                        } else {
                            // 提示修改失败
                            this.$message({
                                message: this.addAndEditDialogTitle + '失败！',
                                type: 'error'
                            });
                        }
                    })
                    // 清空输入框数据
                    this.brand = {};
                } else {
                    // 添加
                    axios({
                        method: "post",
                        url: "http://localhost:8080/brand-case/brand/add",
                        data: this.brand
                    }).then(response => {
                        if (response.data === "success") {
                            // 添加成功
                            // 关闭对话框
                            this.dialogVisible = false;
                            // 刷新数据列表
                            this.selectAll();
                            // 提示添加成功
                            this.$message({
                                message: this.addAndEditDialogTitle + '成功！',
                                type: 'success'
                            });
                        } else {
                            // 提示添加失败
                            this.$message({
                                message: this.addAndEditDialogTitle + '失败！',
                                type: 'error'
                            });
                        }
                    })
                    // 清空输入框数据
                    this.brand = {};
                }
            },

            // 对话框关闭
            dialogClose() {
                this.dialogVisible = false
                // 清空输入框数据
                this.brand = {}
            },

            // 删除单条记录
            deleteById(id) {
                const _this = this;

                this.$confirm('确认删除该品牌信息, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios({
                        method: "post",
                        url: "http://localhost:8080/brand-case/brand/delete",
                        data: id
                    }).then(function (response) {
                        if (response.data === 'success') {
                            // 删除成功
                            // 刷新数据列表
                            _this.selectAll();
                            // 提示删除成功
                            _this.$message({
                                message: '删除品牌成功！',
                                type: 'success'
                            });
                        } else {
                            // 提示删除失败
                            _this.$message({
                                message: '删除品牌失败！',
                                type: 'error'
                            });
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },

            // 删除多条记录
            deleteByIds() {
                const _this = this;

                this.$confirm('确认删除选中的品牌信息, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios({
                        method: "post",
                        url: "http://localhost:8080/brand-case/brand/deleteByIds",
                        data: _this.selectedId
                    }).then(function (response) {
                        if (response.data === 'success') {
                            // 删除成功
                            // 刷新数据列表
                            _this.selectAll();
                            // 提示删除成功
                            _this.$message({
                                message: '删除品牌成功！',
                                type: 'success'
                            });
                        } else {
                            // 提示删除失败
                            _this.$message({
                                message: '删除品牌失败！',
                                type: 'error'
                            });
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },

            // 复选框选中后执行的方法
            handleSelectionChange(val) {
                this.multipleSelection = val;

                // 控制批量删除按钮的启用状态
                if (this.multipleSelection.length > 0) {
                    this.deleteInBatchesBtn = false;
                } else {
                    this.deleteInBatchesBtn = true;
                }

                // 遍历数组对象，获取id
                for (let i = 0; i < this.multipleSelection.length; i++) {
                    // 获取对象
                    let selectionObj = this.multipleSelection[i];
                    // 存储到Id数组中
                    this.selectedId[i] = selectionObj.id;
                }
            },

            // 查询方法
            onSubmit() {
                console.log(this.brand)
                this.selectAll();
            },

            // 分页
            handleSizeChange(val) {
                this.pageSize = val;
                this.selectAll();
            },
            handleCurrentChange(val) {
                this.currentPage = val;
                this.selectAll();
            }

        },

        data() {
            return {
                // 当前页码
                currentPage: 1,

                // 每页记录数
                pageSize: 5,

                // 总记录数
                totalCount: 10,

                // 添加和编辑数据对话框标题
                addAndEditDialogTitle: '',

                // 添加和编辑数据对话框是否展示的标记
                dialogVisible: false,

                // 品牌模型数据
                brand: {
                    status: '',
                    brandName: '',
                    companyName: '',
                    id: "",
                    ordered: "",
                    description: ""
                },

                // 复选框选中数据集合
                multipleSelection: [],

                // 选中的Id数组
                selectedId: [],

                // 批量删除按钮状态
                deleteInBatchesBtn: true,

                // 表格数据
                tableData: [{
                    brandName: '华为',
                    companyName: '华为科技有限公司',
                    ordered: '100',
                    status: "1"
                }, {
                    brandName: '华为',
                    companyName: '华为科技有限公司',
                    ordered: '100',
                    status: "1"
                }, {
                    brandName: '华为',
                    companyName: '华为科技有限公司',
                    ordered: '100',
                    status: "1"
                }, {
                    brandName: '华为',
                    companyName: '华为科技有限公司',
                    ordered: '100',
                    status: "1"
                }]
            }
        }
    })
</script>

</body>
</html>